import {Command} from '../constant';
import _ from 'lodash';
import {IMainReducer} from '../types';
import {Action} from 'typings';
import produce from 'immer';
import * as immerUtil from '@/redux/immer-util';

const INITIAL_STATE: IMainReducer = {
  isReady: false,
  isLoading: false,
  isLogin: false, // 是否已登录状态
  visible: false,
  orderCount: {
    waitPay: 0,
    waitDeliver: 0,
    waitReceiving: 0,
    waitEvaluate: 0,
    refund: 0,
  },
  customer: {},
  pointsAvailable: 0, //积分数
  totalSignDays: 0, //累计签到天数
  isAquamarineMember: false, //是否蓝晶会员标志 false：否，true：是
  isCrystalMember: false, //是否赤晶会员标志 false：否，true：是
  memberDays: null, //已成为会员多少天-会员时才会展示，非会员无需展示
  endTime: '', //会员有效期至
  dualMembershipCardNumber: '', //双会员卡号
  aquamarineMembershipCardNumber: '', //蓝晶会员卡号
  crystalMembershipCardNumber: '', //赤晶会员卡号
  blacklistModel: false, //黑名单弹框
  rankBadgeImg: '', //等级徽章
};

export default function main(state = INITIAL_STATE, action: Action): IMainReducer {
  const {type, payload} = action;

  return produce<IMainReducer>(state, (draftState) => {
    switch (type) {
      //通用改变数据
      case Command.commonChange:
        return immerUtil.commonChange(draftState, {...payload, key: 'main'});

      //初始化
      case Command.init:
        draftState.isReady = true;
        for (let propKey in payload.main) {
          //@ts-ignore 这里处理的不够好.
          draftState[propKey] = payload.main[propKey];
        }
        return draftState;

      //重置
      case Command.clean:
        for (let propKey in INITIAL_STATE) {
          //@ts-ignore 这里处理的不够好.
          draftState[propKey] = INITIAL_STATE[propKey];
        }
        return draftState;
    }
  });
}

//create by moon https://github.com/creasy2010/moon
